package com.culture.bootdemo.controller;

import com.google.common.util.concurrent.RateLimiter;

/**
 * @Author: wen
 * @DateTime: 2024/8/9 11:39
 * @Description: 令牌桶
 **/
public class TokenBucketDemo {

    //线程安全的
    private static RateLimiter rateLimiter = RateLimiter.create(1.0);// 每秒生成10个令牌

    public static void main(String[] args) {

        for (int i = 0; i < 20; i++) {
            double acquire = rateLimiter.acquire();// 请求1个令牌
            System.out.println("Request allowed at: " + System.currentTimeMillis()+"---:"+acquire);
        }
    }
}
